﻿<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
</head>
    <body>
        <input id="Text1" type="text" />
        <input id="Button1" type="button" value="button" onclick=" extractLastWord()"/>

         <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js"></script>
        <script type="text/javascript">

            function extractLastWord() {
                //remove html tags
                term = $("#Text1").val();
                var elementsInTerms = new Array();

                elementsInTerms[0] = ""; //begining element
                elementsInTerms[1] = term; //found word
                elementsInTerms[2] = ""; //ending element
                elementsInTerms[3] = -1; //location for first letter of the found word

                var initialText = term;
                var endingElement = "";
                var foundWord = "";
                var beginningElement = "";
                var isWordFound = false;
                var foundWordStartIndex = -1;

                //var whiteSpaceRegex = /(\s|\n|\r|\t)/g/m;
                var whiteSpaceRegex = /\s|\n|\r|\t/g;

                var i = initialText.length - 1;

                while (i >= 0) {
                    while (!isWordFound) {
                        if (whiteSpaceRegex.test(initialText.charAt(i))) {
                            while (i >= 0) {
                                beginningElement += initialText.charAt(i);
                                i--;
                            }
                            isWordFound = true; //not found here, but needs to break from while loop
                        }
                        else {
                            while (!isWordFound) {
                                while (initialText.charAt(i) != ">" && initialText.charAt(i) != "<" && !whiteSpaceRegex.test(initialText.charAt(i)) && i >= 0) {
                                    foundWord += initialText.charAt(i);
                                    isWordFound = true;
                                    foundWordStartIndex = i;
                                    i--;
                                }
                                if (initialText.charAt(i) == ">") {
                                    while (initialText.charAt(i) != "<" && i >= 0) {
                                        if (isWordFound) {
                                            beginningElement += initialText.charAt(i);
                                        } else {
                                            endingElement += initialText.charAt(i);
                                        }
                                        i--;
                                    }
                                }
                                if (initialText.charAt(i) == "<") {
                                    if (isWordFound) {
                                        beginningElement += initialText.charAt(i);
                                    } else {
                                        endingElement += initialText.charAt(i);
                                    }
                                    i--;
                                }
                                if (whiteSpaceRegex.test(initialText.charAt(i))) {
                                    isWordFound = true; //not found here, but needs to break from while loop
                                }
                            } if (isWordFound) {
                                  while (i >= 0) {
                                      beginningElement += initialText.charAt(i);
                                      i--;
                                  }
                              }
                        }

                    }
                }


                elementsInTerms[0] = beginningElement.split('').reverse().join('');
                elementsInTerms[1] = foundWord.split('').reverse().join('');
                elementsInTerms[2] = endingElement.split('').reverse().join('');
                elementsInTerms[3] = foundWordStartIndex;

                alert("found word: -" + elementsInTerms[1] + "-");

                alert(elementsInTerms);
                return elementsInTerms;
            }

        </script>
    </body>
</html>
